package com.youku.plugins.endcardV4New.model
{
   import com.youku.plugins.endcardV4New.model.data.RelativeVideoList;
   import com.youku.plugins.endcardV4New.model.data.PlayerVideoData;
   import com.youku.interfaces.IPlayerProxy;
   import com.youku.plugins.PluginEventDispather;
   import flash.utils.Timer;
   import flash.net.URLLoader;
   import flash.net.URLRequest;
   import flash.events.Event;
   import flash.events.IOErrorEvent;
   import flash.events.SecurityErrorEvent;
   import com.youku.plugins.endcardV4New.EndCardEvent;
   import flash.events.TimerEvent;
   import com.adobe.json.JSON;
   import com.youku.plugins.endcardV4New.model.data.RelativeVideoData;
   
   public class RelativeVideoListProxy extends Object
   {
      
      public function RelativeVideoListProxy(param1:PluginEventDispather, param2:String = null, param3:Object = null) {
         super();
         this._eventDispather = param1;
         this._relativeVideoList = new RelativeVideoList();
         this._loadTimer = new Timer(10 * 1000);
         this._loadTimer.addEventListener(TimerEvent.TIMER,this.onLoadTimeOut);
      }
      
      private var _relativeVideoList:RelativeVideoList;
      
      private var _playerVideoData:PlayerVideoData;
      
      private var _playerProxy:IPlayerProxy;
      
      private var _eventDispather:PluginEventDispather;
      
      private var _loadTimer:Timer;
      
      private var _urlLoader:URLLoader;
      
      public function loadData(param1:IPlayerProxy) : void {
         this._playerProxy = param1;
         var _loc2_:String = this._playerProxy.playerData.getRelativeURL();
         var _loc3_:URLRequest = new URLRequest(_loc2_);
         this._urlLoader = new URLLoader(_loc3_);
         this._urlLoader.addEventListener(Event.COMPLETE,this.onLoadComplete);
         this._urlLoader.addEventListener(IOErrorEvent.IO_ERROR,this.onError);
         this._urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,this.onError);
         this._urlLoader.load(_loc3_);
         this._playerVideoData = new PlayerVideoData(this._playerProxy);
         this._loadTimer.reset();
         this._loadTimer.start();
      }
      
      private function onError(param1:Event = null) : void {
         this._loadTimer.stop();
         this.meetError();
      }
      
      private function meetError() : void {
         this._eventDispather.dispatchEvent(new EndCardEvent(EndCardEvent.RELATIVE_DATA_ERROR,
            {
               "relativeVideoList":null,
               "playerVideoData":this._playerVideoData
            }));
      }
      
      private function onLoadComplete(param1:Event) : void {
         this._loadTimer.stop();
         var _loc2_:String = URLLoader(param1.currentTarget).data;
         var _loc3_:Boolean = this.parseEndCardData(_loc2_);
         if(_loc3_)
         {
            this._eventDispather.dispatchEvent(new EndCardEvent(EndCardEvent.RELATIVE_DATA_COMPLETE,
               {
                  "relativeVideoList":this._relativeVideoList,
                  "playerVideoData":this._playerVideoData
               }));
         }
         else
         {
            this._eventDispather.dispatchEvent(new EndCardEvent(EndCardEvent.RELATIVE_DATA_ERROR,
               {
                  "relativeVideoList":null,
                  "playerVideoData":this._playerVideoData
               }));
         }
      }
      
      private function onLoadTimeOut(param1:TimerEvent) : void {
         try
         {
            this._urlLoader.close();
         }
         catch(e:Error)
         {
         }
         this._loadTimer.stop();
         this.meetError();
      }
      
      private function parseEndCardData(param1:String) : Boolean {
         var obj:Object = null;
         var datastr:String = param1;
         try
         {
            obj = com.adobe.json.JSON.decode(datastr);
         }
         catch(error:Error)
         {
            return false;
         }
         return this.parseRelativeData(obj);
      }
      
      private function parseRelativeData(param1:Object) : Boolean {
         var _loc4_:Object = null;
         var _loc5_:RelativeVideoData = null;
         var _loc2_:Array = new Array();
         var _loc3_:Array = param1.data;
         if(_loc3_ == null || _loc3_.length == 0)
         {
            return false;
         }
         for each(_loc4_ in _loc3_)
         {
            _loc5_ = new RelativeVideoData();
            if(_loc4_["logo"])
            {
               _loc5_.logo = _loc4_.logo;
            }
            if(_loc4_["videoid"])
            {
               _loc5_.videoID = _loc4_.videoid;
            }
            if(_loc4_["totalPv"])
            {
               _loc5_.totalPV = Number(_loc4_.totalPv);
            }
            if(_loc4_["totalComment"])
            {
               _loc5_.totalComment = _loc4_.totalComment;
            }
            if(_loc4_["username"])
            {
               _loc5_.username = _loc4_.username;
            }
            if(_loc4_["title"])
            {
               _loc5_.title = _loc4_.title;
            }
            if(_loc4_["link"])
            {
               _loc5_.link = _loc4_.link;
            }
            _loc5_.target = "_blank";
            if(_loc4_["seconds"])
            {
               _loc5_.seconds = Number(_loc4_["seconds"]);
            }
            if(_loc4_["dvid"])
            {
               _loc5_.dvid = _loc4_.dvid;
            }
            if(_loc4_["dsid"])
            {
               _loc5_.dsid = _loc4_.dsid;
            }
            if(_loc4_["dma"])
            {
               _loc5_.dma = _loc4_.dma;
            }
            if(_loc4_["cpos"])
            {
               _loc5_.cpos = _loc4_.cpos;
            }
            if(_loc4_["abver"])
            {
               _loc5_.abver = _loc4_.abver;
            }
            if(_loc4_["ct"])
            {
               _loc5_.ct = _loc4_.ct;
            }
            if(_loc4_["dct"])
            {
               _loc5_.dct = _loc4_.dct;
            }
            if(_loc4_["md"])
            {
               _loc5_.md = _loc4_.md;
            }
            if(_loc4_["ord"])
            {
               _loc5_.ord = _loc4_.ord;
            }
            if(_loc4_["rtlid"])
            {
               _loc5_.rtlid = _loc4_.rtlid;
            }
            if(_loc4_["algInfo"])
            {
               _loc5_.algInfo = _loc4_.algInfo;
            }
            _loc2_.push(_loc5_);
         }
         this._relativeVideoList.videoList = _loc2_;
         this._relativeVideoList.isDataReady = true;
         return true;
      }
   }
}
